"""
Bar线相关的数据类，有生成Bar线，Bar线序列
"""
import numpy as np
import pandas as pd

from dytz.data import *

__all__ = ['Bar', 'BarSeries']


class Bar:
    """每根Bar线的类"""

    def __init__(self, data_series: pd.Series):
        """
        构造器
        :param data_series: 包含每天信息的序列
        """
        self.TICKER = data_series.TICKER
        self.date = data_series.name
        self.OPEN = data_series.OPEN
        self.CLOSE = data_series.CLOSE
        self.HIGH = data_series.HIGH
        self.LOW = data_series.LOW
        self.VOLUME = data_series.VOLUME
        if np.isnan(data_series.AVGPRICE):
            self.AVGPRICE = (self.OPEN + self.CLOSE + self.HIGH + self.LOW) / 4
        else:
            self.AVGPRICE = data_series.AVGPRICE


class BarSeries:
    """Bar线时间序列"""

    def __init__(self, n: int):
        """
        构造器
        :param n: 序列长度
        """
        self.H = TimeSeries(n)
        self.L = TimeSeries(n)
        self.C = TimeSeries(n)
        self.O = TimeSeries(n)

    def on_bar(self, bar: Bar) -> None:
        """
        每根bar线更新时的计算情况
        :param bar: Bar线
        :return: None
        """
        self.H.step()
        self.L.step()
        self.C.step()
        self.O.step()
        self.H.set(bar.HIGH)
        self.L.set(bar.LOW)
        self.C.set(bar.CLOSE)
        self.O.set(bar.OPEN)
